﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cocbook_DTO;
using Cocbook_DAL;

namespace Cocbook_DAL
{
    public class Order_BookDAL
    {
        public bool CreateOrder_Book(Order_Book ob)
        {
            DB.SetQuery("INSERT INTO Order_Book VALUES(@oID, @bID, @dis, @copy)");
            DB.AddParameters("oID", ob.Order.OrderID);
            DB.AddParameters("bID", ob.Book.BookID);
            DB.AddParameters("dis", ob.Discount);
            DB.AddParameters("copy", ob.Copy);
            try
            {
                DB.ExeNoneQuery();
                return true;
            }
            catch (Exception e)
            {
                Logger.Instance.Log("Program.Main.Test.CreateOrder_Book", e.ToString());
                return false;
            }
        }

        public bool UpdateOrder_Book(Order_Book ob)
        {
            DB.SetQuery("UPDATE Order_Book SET Discount = @dis, Copy = @copy WHERE (OrderID = @oID)and(BookID = @bID)");
            DB.AddParameters("oID", ob.Order.OrderID);
            DB.AddParameters("bID", ob.Book.BookID);
            DB.AddParameters("dis", ob.Discount);
            DB.AddParameters("copy", ob.Copy);
            try
            {
                DB.ExeNoneQuery();
                return true;
            }
            catch (Exception e)
            {
                Logger.Instance.Log("Program.Main.Test.UpdateOrder_Book", e.ToString());
                return false;
            }
        }

        public bool DeleteOrder_Book(int oId, int bId)
        {
            DB.SetQuery("DELETE FROM Order_Book WHERE (OrderID = @oID) and (BookID = @bID)");
            DB.AddParameters("oID", oId);
            DB.AddParameters("bID", bId);
            try
            {
                DB.ExeNoneQuery();
                return true;
            }
            catch (Exception e)
            {
                Logger.Instance.Log("Program.Main.Test.DeleteOrder_Book", e.ToString());
                return false;
            }
        }

        public List<Order_Book> GetOrder_BookByBookID(int id)
        {
            try
            {
                DB.SetQuery("SELECT OrderID, BookID, Discount, Copy FROM Order_Book WHERE BookID = @id");
                DB.AddParameters("id", id);
                List<List<object>> llo = new List<List<object>>();
                llo = DB.ExeQuery();
                List<Order_Book> OB = new List<Order_Book>();
                Order_Book ob = new Order_Book();
                if (llo.Count > 0)
                {
                    foreach (List<object> lo in llo)
                    {
                        ob = new Order_Book();
                        ob.Order = (new OrderDAL()).GetOrderByID(Convert.ToInt16(lo[0]));
                        ob.Book = (new BookDAL()).GetBookById(Convert.ToInt16(lo[1]));
                        ob.Discount = Convert.ToDouble(lo[2]);
                        ob.Copy = Convert.ToInt16(lo[3]);
                        OB.Add(ob);
                    }
                }
                else "".Substring(2);
                
                return OB;
            }
            catch (Exception e)
            {
                Logger.Instance.Log("Program.Main.Test.GetOrder_BookByID", e.ToString());
                return null;
            }
        }

        public List<Order_Book> GetOrder_BookByOrderID(int id)
        {
            try
            {
                DB.SetQuery("SELECT OrderID, BookID, Discount, Copy FROM Order_Book WHERE OrderID = @id");
                DB.AddParameters("id", id);
                List<List<object>> llo = new List<List<object>>();
                llo = DB.ExeQuery();
                List<Order_Book> OB = new List<Order_Book>();
                Order_Book ob = new Order_Book();
                if (llo.Count > 0)
                {
                    foreach (List<object> lo in llo)
                    {
                        ob = new Order_Book();
                        ob.Order = (new OrderDAL()).GetOrderByID(Convert.ToInt16(lo[0]));
                        ob.Book = (new BookDAL()).GetBookById(Convert.ToInt16(lo[1]));
                        ob.Discount = Convert.ToDouble(lo[2]);
                        ob.Copy = Convert.ToInt16(lo[3]);
                        OB.Add(ob);
                    }
                }
                else "".Substring(2);

                return OB;
            }
            catch (Exception e)
            {
                Logger.Instance.Log("Program.Main.Test.GetOrder_BookByBookID", e.ToString());
                return null;
            }
        }

        public List<Order_Book> GetAllOrder_Book()
        {
            try
            {
                DB.SetQuery("SELECT OrderID, BookID, Discount, Copy FROM Order_Book");
                List<List<object>> llo = new List<List<object>>();
                llo = DB.ExeQuery();
                List<Order_Book> OB = new List<Order_Book>();
                Order_Book ob = new Order_Book();
                if (llo.Count > 0)
                {
                    foreach (List<object> lo in llo)
                    {
                        ob = new Order_Book();
                        ob.Order = (new OrderDAL()).GetOrderByID(Convert.ToInt16(lo[0]));
                        ob.Book = (new BookDAL()).GetBookById(Convert.ToInt16(lo[1]));
                        ob.Discount = Convert.ToDouble(lo[2]);
                        ob.Copy = Convert.ToInt16(lo[3]);
                        OB.Add(ob);
                    }
                }
                else "".Substring(2);

                return OB;
            }
            catch (Exception e)
            {
                Logger.Instance.Log("Program.Main.Test.GetAllOrder_Book", e.ToString());
                return null;
            }
        }
    }
}
